package live.testdemo.bazi.common.constants;

import java.util.Arrays;

import lombok.AllArgsConstructor;
import lombok.Getter;

import static live.testdemo.bazi.common.constants.DiZhi.丑;
import static live.testdemo.bazi.common.constants.DiZhi.亥;
import static live.testdemo.bazi.common.constants.DiZhi.午;
import static live.testdemo.bazi.common.constants.DiZhi.卯;
import static live.testdemo.bazi.common.constants.DiZhi.子;
import static live.testdemo.bazi.common.constants.DiZhi.寅;
import static live.testdemo.bazi.common.constants.DiZhi.巳;
import static live.testdemo.bazi.common.constants.DiZhi.戌;
import static live.testdemo.bazi.common.constants.DiZhi.未;
import static live.testdemo.bazi.common.constants.DiZhi.申;
import static live.testdemo.bazi.common.constants.DiZhi.辰;
import static live.testdemo.bazi.common.constants.DiZhi.酉;
import static live.testdemo.bazi.common.constants.WuXing.土;
import static live.testdemo.bazi.common.constants.WuXing.木;
import static live.testdemo.bazi.common.constants.WuXing.水;
import static live.testdemo.bazi.common.constants.WuXing.火;
import static live.testdemo.bazi.common.constants.WuXing.金;

/**
 * Created by chentz on 2017/11/17.
 * 天干
 * 甲木、乙木、丙火、丁火、戊土、己土、庚金、辛金、壬水、癸水，其中甲丙戊庚壬为阳性，乙丁己辛癸为阴性
 */
@Getter
@AllArgsConstructor
public enum TianGan {
    甲(1, 木, new DiZhi[]{寅, 卯, 辰, 未, 亥}),
    乙(2, 木, new DiZhi[]{寅, 卯, 辰, 未, 亥}),
    丙(3, 火, new DiZhi[]{寅, 巳, 午, 未, 戌}),
    丁(4, 火, new DiZhi[]{寅, 巳, 午, 未, 戌}),
    戊(5, 土, new DiZhi[]{寅, 辰, 巳, 午, 未, 申, 戌, 丑}),
    己(6, 土, new DiZhi[]{寅, 辰, 巳, 午, 未, 申, 戌, 丑}),
    庚(7, 金, new DiZhi[]{巳, 申, 酉, 戌, 丑}),
    辛(8, 金, new DiZhi[]{巳, 申, 酉, 戌, 丑}),
    壬(9, 水, new DiZhi[]{辰, 申, 亥, 子, 丑}),
    癸(10, 水, new DiZhi[]{辰, 申, 亥, 子, 丑});

    private int id;

    private WuXing wuXing;

    private DiZhi[] gen;

    /**
     * 获取相同五行的天干
     *
     * @param tianGan
     * @return
     */
    public static TianGan getTongWuXing(TianGan tianGan) {
        return Arrays.stream(TianGan.values()).filter(x -> x.getWuXing().equals(tianGan.getWuXing()))
                .findFirst().get();
    }

    /**
     * 获取相同五行的天干
     *
     * @param tianGan
     * @return
     */
    public static TianGan getTongWuXing(String tianGan) {
        return Arrays.stream(TianGan.values()).filter(x -> x.getWuXing().equals(TianGan.valueOf(tianGan).getWuXing()))
                .findFirst().get();
    }

    /**
     * 根据索引获取天干
     *
     * @param id
     * @return
     */
    public static TianGan getTianGan(int id) {
        if (id < 0 || id > 9) {
            return null;
        }
        TianGan[] tianGans = TianGan.values();
        for (int i = 0; i < tianGans.length; i++) {
            if (tianGans[i].getId() == id) {
                return tianGans[i];
            }
        }
        return null;
    }

    /**
     * 同类型的干
     *
     * @param gan 格式为天干地支 例如 丙寅
     * @return
     */
    public boolean isTongWuXing(String gan) {
        TianGan tongWuXing = getTongWuXing(gan);
        return this.getWuXing().equals(tongWuXing.getWuXing());
    }

    /**
     * 判断某一柱是否是当前天干的同类
     *
     * @param ganZhi 格式为天干地支 例如 丙寅
     * @return
     */
    public boolean isTongLei(String ganZhi) {
        TianGan tongWuXing = getTongWuXing(ganZhi.substring(0, 1));
        return this.getWuXing().equals(tongWuXing.getWuXing()) || this.isTongGen(ganZhi.substring(1, 2));
    }

    /**
     * 判断是否通根于某地支
     *
     * @param diZhi
     * @return
     */
    public boolean isTongGen(DiZhi diZhi) {
        return Arrays.stream(this.getGen()).anyMatch(x -> x.name().equals(diZhi.name()));
    }

    /**
     * 判断是否通根于某地支
     *
     * @param diZhi
     * @return
     */
    public boolean isTongGen(String diZhi) {
        return Arrays.stream(this.getGen()).anyMatch(x -> x.name().equals(DiZhi.valueOf(diZhi).name()));
    }

}
